package com.zl.gmadmin.controller;

import com.zl.gmadmin.utils.FileProperties;
import com.zl.gmadmin.utils.FileUtil;
import com.zl.gmadmin.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ZhangLiang
 * @Date: 2021/08/25/14:13
 * @Description:
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    FileProperties fileProperties;

    /**
     * 上传到本地指定文件下完成
     * @param multipartFile
     * @return
     */
    @PostMapping("image")
    public String uploadImage(@RequestParam("file") MultipartFile multipartFile) {
        FileProperties.ElPath path = fileProperties.getPath();
        String pathUtl = path.getPath().replace("\\","/");

        File file = FileUtil.upload(multipartFile, pathUtl);
        Map<String,Object> map=new HashMap<>();
        map.put("location","/file/"+file.getName());
        return JsonUtil.toJsonStr(map);
    }

}
